package base;


import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

public class test{

	public  void upload(HttpServletRequest request) throws IOException {
		FileDialog file = new FileDialog(new Frame(), "打开", FileDialog.LOAD);//打开目录选择
		file.setVisible(true);
		String rootpath=request.getSession().getServletContext().getRealPath("/");
		String dir = file.getDirectory();//文件根目录
		String name = file.getFile();//文件名
		String output=rootpath+getPath();
		//String output = rootpath+getPath();//导出的目录
		InputStream is = new FileInputStream(dir + name);//把选择的文档输入输入流
		File filename = new File(output);//输出目录
		if(!filename.exists()){
			filename.mkdirs();
		}
		OutputStream os = new FileOutputStream(filename+"/"+getFileName());//输出流
		byte buffer[] = new byte[4 * 1024];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);//输出
		}
		os.flush();//缓冲区的数据强行输出
		os.close();//关闭
	}

	
	public String getPath(){
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
		String date=df.format(new Date());
		return date.substring(0, 4)+"/"+date.substring(4, 6)+"/"+date.substring(6, 8)+"/";
	}
	public String getFileName(){
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
		String date=df.format(new Date());
		return date+".txt";
	}

}